<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_Subjects extends CI_Model {

    private $tmp = '';

	function __construct() {
        parent::__construct();
    }

    public function get_subjects() {
        $query = $this->db->get('subjects');
        return $query->result_array();
    }

    public function get_one_subject($id = 0) {
        $query = $this->db->query('SELECT * FROM subjects WHERE id = "' . $id . '" ');
        return $query->result_array();
    }

    public function insert_subjects($subjects = array()) {

        $subjects = preg_split("/[,]/", $subjects);
        $subjects = array_diff($subjects, array(''));
        $subjects = array_map('trim', $subjects);
        $subjects = array_unique($subjects);

        foreach ($subjects as $names) {
            $query = $this->db->query('SELECT name FROM subjects WHERE name = "' . $names . '" ');
            if ($query->num_rows() > 0) {
                $this->tmp .= $names . ',';
            }
        }

        if ($this->tmp == '') {
            foreach ($subjects as $names2) {
                $this->db->insert('subjects', array('name' => $names2));
            }
            return lang('successfull');
        } else {
            return sprintf(lang('admin_subject_required'), $this->tmp);
        }
    }

    public function edit_subject($id = 0, $name = '') {
    	$query = $this->db->query('SELECT id,name FROM subjects WHERE name = "' . $name . '" ');
        if ($query->num_rows() > 0) {
        	$row = $query->row();
        	if ($row->id != $id) {
            	return sprintf(lang('admin_subject_required'), $name);
            }
            else
            {
	        	$this->db->update('subjects', array(
	        	'name'=>$name), array('id' => $id));
	        	return lang('successfull');
            }
        }
        else
        {
	        $this->db->update('subjects', array(
	        'name'=>$name), array('id' => $id));
	        return lang('successfull');
        }
    }

    public function delete_subject($subjects = array()) {

        foreach ($subjects as $id) {
            $this->db->delete('subjects', array('id' => $id));
        }
    }

}

/* End of file m_admin.php */
/* Location: ./application/models/m_admin.php */